package com.kestrel.space.core.controller;

import com.kestrel.space.common.controller.BaseController;
import com.kestrel.space.common.utils.LargePage;
import com.kestrel.space.common.utils.Result;
import com.kestrel.space.core.model.Blog;
import com.kestrel.space.core.query.BlogQuery;
import com.kestrel.space.core.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author yebolin
 * @date 2024/8/23
 */
@RestController
@RequestMapping("blog")
public class BlogController extends BaseController {
    @Autowired
    private BlogService blogService;

    /**
     * 增删改查全表（内部）
     * @param query
     * @return
     */
    @RequestMapping("listContent")
    public Result listContent(@RequestBody BlogQuery query){
        return Result.list(blogService.listContent(query, new LargePage(query.getPage(), query.getRows())));
    }
    @RequestMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Blog blog){
        return Result.ok(blogService.saveOrUpdate(blog));
    }
    @RequestMapping("batchSaveOrUpdate")
    public Result batchSaveOrUpdate(@RequestBody List<Blog> blogList){
        return Result.ok(blogService.batchSaveOrUpdate(blogList));
    }

    /**
     * 编辑博客
     * 实际上是替换原文件（含pdf），重新生成pdf并存储
     * 组织内部文档暂时无法公共编辑，需要文档原作者进行编辑重传
     * @param file
     * @param id
     * @return
     */
    @RequestMapping("editBlogs")
    public Result editBlogs(@RequestPart("file") MultipartFile file, @RequestParam Long id){
        return Result.ok(blogService.editBlogs(file, id));
    }

    /**
     * 公网搜索博客
     * @param query
     * @return
     */
    @RequestMapping("listPublicBlogs")
    public Result listPublicBlogs(@RequestBody BlogQuery query){
        return Result.ok(blogService.listPublicBlogs(query));
    }

    /**
     * 列出组织内部文档
     * @param query
     * @return
     */
    @RequestMapping("listOrganizationBlogs")
    public Result listOrganizationBlogs(@RequestBody BlogQuery query){
        return Result.ok(blogService.listOrganizationBlogs(query));
    }

    /**
     * 把博客分配/取消分配给组织
     * 个人网盘进行分配
     * 组织中进行取消分配（超管和上传者可以取消）
     * @param pdfId
     * @param organizationId
     * @return
     */
    @RequestMapping("allocate")
    public Result allocate(@RequestParam Long pdfId, @RequestParam Long organizationId, @RequestParam boolean stage){
        return Result.ok(blogService.allocate(pdfId, organizationId, stage));
    }

    /**
     * 展示组织中我的博客编号
     * @return
     * @throws Exception
     */
    @RequestMapping("listMyBlogsInOrg")
    public Result listMyBlogsInOrg(@RequestParam Long organizationId) throws Exception {
        return Result.ok(blogService.listMyBlogsInOrg(getUser(), organizationId));
    }

    /**
     * 下载文件
     * 此 id为 docxId or pdfId
     * @param id
     */
    @RequestMapping("download")
    public Result downloadBlog(@RequestParam Long id) {
        return Result.ok(blogService.downloadBlog(id, request, response));
    }

    /**
     * 逻辑删除博客
     */
    @RequestMapping("delete")
    public Result deleteBlog(@RequestParam Long id){
        return Result.ok(blogService.deleteBlog(id));
    }

    // 上传博客接口为file模块的同步接口

    /**
     * 展示博客内容
     * Blog + File (不含Comment Like)
     * @param id
     * @return
     */
    @RequestMapping("showBlogsDetail")
    public Result showBlogsDetail(@RequestParam Long id){
        return Result.ok(blogService.showBlogsDetail(id));
    }


}
